<?php
/*
 * class for   update user
 * If creation is successful, the browser will be redirected to the 'view' page.
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

class UpdateAction extends CAction {

   public function run() {
      $controller = $this->getController();
      $model = User::model()->loadModel($_GET['id']);
      $picture = $model->picture;
      if (isset($_POST['User'])) {

         $model->attributes = $_POST['User'];
         if (!$model->picture)
            $model->picture = $picture;
         $paths = $model->createImageDirectories($_GET['id']);

         $modelPicture = CUploadedFile::getInstance($model, 'picture');

         if (isset($modelPicture) && !empty($modelPicture)) {
            if (is_file(Yii::app()->params['imageSavePath'] . $model->picture)) {
               unlink(Yii::app()->params['imageSavePath'] . $model->picture);
            }
            $rand = md5($model->generateRandomString());
            $imageType = explode('image/', $modelPicture->getType());
            $file = Yii::app()->params['imageSavePath'] . $paths['thumbnail'] . DIRECTORY_SEPARATOR . $rand . '.' . $imageType[1];
            if ($modelPicture->saveAs($file))
               $img = Yii::app()->simpleImage->load($file);
            $img->resizeToWidth(128);
            $img->save($file);
            $model->picture = $paths['thumbnail'] . DIRECTORY_SEPARATOR . $rand . '.' . $imageType[1];
         }
         if ($model->save())
            Yii::app()->user->setFlash('success', "User Updated successfully!!");
         $controller->redirect(array('view', 'id' => $model->id));
      }

      $controller->render('update', array(
          'model' => $model,
      ));
   }
}
